;+function(){
	$.fn.formatBankCard = function(){
		this.each(function(){
			var self = $(this);
			self.on('keypress',function(e){
				formatCardNum(e);
			})
			self.on("blur",function(e){
				trimValue(e);
			})
			self[0].onpaste = function(e){
				var e = e ||window.event;
				var me = e.currentTarget || e.srcElement;
				if(!!!window.VBArray){
					 pasteChrome(e,me);
				}else if(!!window.VBArray){
					pasteIE(e,me);
				}
			}
			self[0].ondrop = function(e){
				e.preventDefault();
			}
		})

		function pasteChrome(e,me){
			e.preventDefault();
			var chromeStr = e.clipboardData.getData('text/plain');
			var patt = /[^\d]/g;
			chromeStr = chromeStr.replace(patt,'');
			var arr = chromeStr.split("");
			for(var i = arr.length;i--;){
				if(i%4==3){
					var t = arr[i];
					arr.splice(i,1,t," ");
				}
			}
			chromeStr = arr.join("");
			me.value = chromeStr;
			if(me.value.length>24){
				me.value = me.value.slice(0,24);
			}
		}
		function pasteIE(e,me){
			e.preventDefault();
			var ieStr = window.clipboardData.getData('Text');
			var patt = /[^\d]/g;
			ieStr = ieStr.replace(patt,'');
			var arr = ieStr.split("");
			for(var i = arr.length;i--;){
				if(i%4==3){
					var t = arr[i];
					arr.splice(i,1,t," ");
				}
			}
			ieStr = arr.join("");
			me.value = ieStr;
			if(me.value.length>24){
				me.value = me.value.slice(0,24);
			}
		}
		function formatCardNum(e){
			var me = e.currentTarget;
			var reg = /\s/g;
			var code = e.which;
			var value = me.value.replace(reg,'');
			if(
				code!=48&&
				code!=49&&
				code!=50&&
				code!=51&&
				code!=52&&
				code!=53&&
				code!=54&&
				code!=55&&
				code!=56&&
				code!=57
			){
				e.preventDefault();
			}else{
				if(value.length % 4==0){
					me.value = me.value + ' ';
				};
				if(me.value.length>=24){
					me.value = me.value.slice(0,24);
				}
			}

		}
		function trimValue(e){
			var me = e.currentTarget;
			me.value = $.trim(me.value);
		}
	}
}(jQuery)
